package me.goldze.mvvmhabit.binding.viewadapter.scrollview;

import android.databinding.BindingAdapter;
import android.support.v4.widget.NestedScrollView;
import android.view.ViewTreeObserver;
import android.widget.ScrollView;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

public final class ViewAdapter {
    @BindingAdapter({"onScrollChangeCommand"})
    public static void onScrollChangeCommand(NestedScrollView nestedScrollView, final BindingCommand<NestScrollDataWrapper> onScrollChangeCommand) {
        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            /* class me.goldze.mvvmhabit.binding.viewadapter.scrollview.ViewAdapter.AnonymousClass1 */

            @Override // android.support.v4.widget.NestedScrollView.OnScrollChangeListener
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                BindingCommand bindingCommand = onScrollChangeCommand;
                if (bindingCommand != null) {
                    bindingCommand.execute(new NestScrollDataWrapper(scrollX, scrollY, oldScrollX, oldScrollY));
                }
            }
        });
    }

    @BindingAdapter({"onScrollChangeCommand"})
    public static void onScrollChangeCommand(final ScrollView scrollView, final BindingCommand<ScrollDataWrapper> onScrollChangeCommand) {
        scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            /* class me.goldze.mvvmhabit.binding.viewadapter.scrollview.ViewAdapter.AnonymousClass2 */

            public void onScrollChanged() {
                BindingCommand bindingCommand = onScrollChangeCommand;
                if (bindingCommand != null) {
                    bindingCommand.execute(new ScrollDataWrapper((float) scrollView.getScrollX(), (float) scrollView.getScrollY()));
                }
            }
        });
    }

    public static class ScrollDataWrapper {
        public float scrollX;
        public float scrollY;

        public ScrollDataWrapper(float scrollX2, float scrollY2) {
            this.scrollX = scrollX2;
            this.scrollY = scrollY2;
        }
    }

    public static class NestScrollDataWrapper {
        public int oldScrollX;
        public int oldScrollY;
        public int scrollX;
        public int scrollY;

        public NestScrollDataWrapper(int scrollX2, int scrollY2, int oldScrollX2, int oldScrollY2) {
            this.scrollX = scrollX2;
            this.scrollY = scrollY2;
            this.oldScrollX = oldScrollX2;
            this.oldScrollY = oldScrollY2;
        }
    }
}
